package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.config.PageInfo;
import cn.wolfcode.rbac.request.EmployeeQueryRequest;
import cn.wolfcode.rbac.request.EmployeeRequest;
import cn.wolfcode.rbac.service.DepartmentService;
import cn.wolfcode.rbac.service.EmployeeService;
import cn.wolfcode.rbac.service.RoleService;
import cn.wolfcode.rbac.util.JsonData;
import cn.wolfcode.rbac.vo.EmployeeQueryVO;
import cn.wolfcode.rbac.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;



/**
 * @author Lu.F
 * @version 1.0
 * @Description TODO
 * @date 2022/5/19 15:42
 */
//@RestController
@Api(tags = "员工模块")
@Controller
//@RequestMapping("/api/employee/v1")
@RequestMapping("employee")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;


    /**
     * 修改list查询方法中的请求对象,改成拓展后的EmployeeQueryObject
     * @param model
     * @param queryRequest
     * @return
     */
    @RequestMapping("/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(Model model,@ModelAttribute("qr") EmployeeQueryRequest queryRequest){
        //员工的高级分页查询
        PageInfo<EmployeeQueryVO> pageInfo = employeeService.list(queryRequest.getCurrentPage(), queryRequest.getPageSize(),queryRequest);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departments",departmentService.selectAll());
        return "employee/list";
    }


    //修改input方法,回显部门信息
    @RequestMapping("/input")
    @RequiredPermission({"员工编辑","employee:input"})
    public String input(Model model,Long id){
        //编辑/添加操作的用户回显数据
        model.addAttribute("employee",employeeService.getVO(id));
        //显示部门信息
        model.addAttribute("departments",departmentService.selectAll());
        // 显示权限信息
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }



    //添加保存/编辑方法
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"员工保存或更新","employee:saveOrUpdate"})
    public String saveOrUpdate(EmployeeRequest employeeRequest){
        employeeService.saveOrUpdate(employeeRequest, employeeRequest.getRoleIds());
        return "redirect:/employee/list";
    }


    @RequestMapping("/delete")
    @RequiredPermission({"员工删除","employee:delete"})
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }

   /* *//**
     * 分页查询用户信息
     * @return
     *//*
    @ApiOperation("分页查询用户信息")
    @GetMapping("/page")
    public JsonData listEmployee(@ApiParam("当前页") @RequestParam(value = "page",defaultValue = "1")int page,
                                 @ApiParam("每页显示条数") @RequestParam(value = "size",defaultValue = "5") int size){

        Map<String, Object> map = employeeService.listEmployee(page, size);
        return JsonData.buildSuccess(map);
    }


    *//**
     * 保存或者更新员工
     * @param employeeRequest
     * @return
     *//*
    @ApiOperation("保存或者更新")
    @PostMapping("/saveOrUpdate")
    public JsonData saveOrUpdate(@ApiParam("员工请求对象")@RequestBody EmployeeRequest employeeRequest){
        employeeService.saveOrUpdate(employeeRequest, employeeRequest.getRoleIds());
        return JsonData.buildSuccess();
    }


    *//**
     * 高级查询员工信息
     * @param employeeQueryRequest
     * @return
     *//*
    @ApiOperation("高级查询员工信息")
    @PostMapping("/list")
    public JsonData list(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "5")int size, @ApiParam("高级查询员工对象")@RequestBody EmployeeQueryRequest employeeQueryRequest){
        Map<String,Object> employeeMaps =  employeeService.list(page,size,employeeQueryRequest);
        return JsonData.buildSuccess(employeeMaps);
    }


    *//**
     * 查看用户展示对象
     * @param id
     * @return
     *//*
    @ApiOperation("查看用户展示对象")
    @GetMapping("getvo/{id}")
    public JsonData getVO(@PathVariable("id")Long id){
        return JsonData.buildSuccess(employeeService.getVO(id));
    }

    *//**
     * 删除用户
     * @param id
     * @return
     *//*
    @ApiOperation("删除用户")
    @GetMapping("delete/{id}")
    public JsonData delete(@PathVariable("id")Long id){
        employeeService.delete(id);
        return JsonData.buildSuccess();
    }*/

}
